# CmakeLists.txt
cmake_minimum_required(VERSION 3.28) # TODO: 修改为更加具有可移植性的构建文件
project(frontend)

message(STATUS "C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC OFF)

# 设置 CMake 路径
set(CMAKE_PREFIX_PATH "D:/AppData/Qt6/Qt/6.7.0/msvc2019_64;D:/GitWork/chat_system/frontend/vcpkg/installed/x64-windows")
set(QT_CMAKE_PREFIX_PATH "D:/AppData/Qt6/Qt/6.7.0/msvc2019_64")
set(VCPKG_CMAKE_PREFIX_PATH "D:/GitWork/chat_system/frontend/vcpkg/installed/x64-windows")

set(RESOURCE_FILES resources/resources.qrc) # 添加资源文件

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" CACHE STRING "Toolchain file used for cross-compiling")

file(GLOB PROTO_FILES
        src/proto/cpp_out/*.pb.h
        src/proto/cpp_out/*.pb.cc
)

# 设置代码文件
set(SRC_FILES
        src/proto/ProtoManager.hpp
        src/MainQwidget.hpp
        src/common/Common.hpp
        src/common/Tools.hpp
        src/common/Type.hpp
        src/model/ChatSessionModel.hpp
        src/model/MessageModel.hpp
        src/model/UserModel.hpp
        src/model/Model.hpp
        src/window/WindowLeft.hpp
        src/window/WindowMiddle.hpp
        src/window/WindowRight.hpp
        src/window/UserLogin.hpp
        src/window/Window.hpp
        src/subassembly/SessionFriendArea/SessionFriendArea.hpp
        src/subassembly/SessionFriendArea/SessionFriendItem.hpp
        src/subassembly/MessageShowArea/MessageShowArea.hpp
        src/subassembly/MessageEditArea/MessageEditArea.hpp
        src/subassembly/TextMessageBubble/TextMessageBubble.hpp
        src/subassembly/SelfInfoWidget/SelfInfoWidget.hpp
        src/subassembly/OtherInfoWidget/OtherInfoWidget.hpp
        src/subassembly/SessionDetailWidget/SessionDetailWidget.hpp
        src/subassembly/SessionDetailWidget/GroupChatSessionDetailWidget.hpp
        src/subassembly/Toast/Toast.hpp
        src/subassembly/ChooseFriendDialog/ChooseFriendDialog.hpp
        src/subassembly/ChooseFriendDialog/Container.hpp
        src/subassembly/ChooseFriendDialog/HaveContainer.hpp
        src/subassembly/ChooseFriendDialog/SelectedContainer.hpp
        src/subassembly/SessionDetailWidget/DoubleChatSessionDetailWidget.hpp
)

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED
)

find_package(fmt CONFIG REQUIRED)

find_package(GTest CONFIG REQUIRED)

find_package(spdlog CONFIG REQUIRED)

find_package(jsoncpp CONFIG REQUIRED)

find_package(Protobuf CONFIG REQUIRED)

add_executable(${PROJECT_NAME}
        ${RESOURCE_FILES} # 将资源文件添加到可执行文件中
        ${PROTO_FILES} # 将 proto 输出文件添加到可执行文件中
        ${SRC_FILES} # 将所有代码文件添加到可执行文件中
        src/main.cpp
)

target_link_libraries(${PROJECT_NAME} PRIVATE # PRIVATE 可以避免报错, 以后研究
        Qt::Core
        Qt::Gui
        Qt::Widgets
)

target_link_libraries(${PROJECT_NAME} PRIVATE
        fmt::fmt
)

target_link_libraries(${PROJECT_NAME} PRIVATE
        GTest::gtest
        GTest::gtest_main
)

target_link_libraries(${PROJECT_NAME} PRIVATE
        spdlog::spdlog
)

target_link_libraries(${PROJECT_NAME} PRIVATE
        JsonCpp::JsonCpp
)

target_link_libraries(${PROJECT_NAME} PRIVATE
        protobuf::libprotoc
        protobuf::libprotobuf
        protobuf::libprotobuf-lite
)


# 所有安装的 Qt 库
set(QT_LIB_LIST Core Gui Widgets)

# 所有通过 vcpkg 安装的库
set(VCPKG_LIB_LIST fmt)

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${QT_CMAKE_PREFIX_PATH}") # 修改
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()

    # 删除拷贝循环
    #    foreach (QT_LIB Core Gui Widgets)
    #        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    #                COMMAND ${CMAKE_COMMAND} -E copy
    #                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
    #                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    #    endforeach (QT_LIB)

    # Qt 新版样式库
    if (EXISTS "${QT_INSTALL_PATH}/plugins/styles/qmodernwindowsstyle${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/styles/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/styles/qmodernwindowsstyle${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/styles/")
    endif ()

    # 用于拷贝 Qt 提供的库到项目构建文件夹
    foreach (QT_LIB ${QT_LIB_LIST})
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)

    # 用于拷贝 vcpkg 提供的库到项目构建文件夹
    foreach (VCPKG_LIB ${VCPKG_LIB_LIST})
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${VCPKG_CMAKE_PREFIX_PATH}/debug/bin/${VCPKG_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (VCPKG_LIB)
endif ()
